package com.wuuxiang.dynamic.mongo.database;

import com.mongodb.ClientSessionOptions;
import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoDatabase;
import org.bson.codecs.configuration.CodecRegistry;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.mongodb.MongoDatabaseFactory;

public abstract class AbstractDynamicMongoDatabaseFactory implements MongoDatabaseFactory {
    protected final DynamicMongoDatabaseFactoryProvider dynamicMongoDatabaseFactoryProvider;

    public AbstractDynamicMongoDatabaseFactory(DynamicMongoDatabaseFactoryProvider dynamicMongoDatabaseFactoryProvider) {
        this.dynamicMongoDatabaseFactoryProvider = dynamicMongoDatabaseFactoryProvider;
    }

    public abstract MongoDatabaseFactory determineMongoDatabaseFactory();

    @Override
    public MongoDatabase getMongoDatabase() throws DataAccessException {
        return determineMongoDatabaseFactory().getMongoDatabase();
    }

    @Override
    public MongoDatabase getMongoDatabase(String dbName) throws DataAccessException {
        return determineMongoDatabaseFactory().getMongoDatabase(dbName);
    }

    @Override
    public PersistenceExceptionTranslator getExceptionTranslator() {
        return determineMongoDatabaseFactory().getExceptionTranslator();
    }

    @Override
    public ClientSession getSession(ClientSessionOptions options) {
        return determineMongoDatabaseFactory().getSession(options);
    }

    @Override
    public MongoDatabaseFactory withSession(ClientSession session) {
        return determineMongoDatabaseFactory().withSession(session);
    }

    @Override
    public CodecRegistry getCodecRegistry() {
        return determineMongoDatabaseFactory().getCodecRegistry();
    }

    @Override
    public MongoDatabaseFactory withSession(ClientSessionOptions options) {
        return determineMongoDatabaseFactory().withSession(options);
    }

    @Override
    public boolean isTransactionActive() {
        return determineMongoDatabaseFactory().isTransactionActive();
    }

}
